using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

[CustomEditor(typeof(HMI_Toggle), true)]
public class HMI_Toggle_Editor : HMI_EditorBase
{
    private int padding_left = 136;
    private int padding_space = 12;
    private int follow_space = 32;
    private HMI_Toggle mTarget;
    private VerticalLayoutGroup mLeftLayoutGroup;
    private VerticalLayoutGroup mSpaceLayoutGroup;
    private MyText mText;
    private bool foldoutText = false;
    private MyTextEditor textEditor;

    private MyText mSubText;
    private bool foldoutSubText = false;
    private MyTextEditor subTextEditor;

    private Toggle mToggle;

    private bool hasFollow; //跟随图标
    private GameObject followObj;

    private bool hasLoading; //转圈图标
    private GameObject loadingObj;

    public override void Awake()
    {
        base.Awake();
        mTarget = target as HMI_Toggle;
        var textZone = mTarget.transform.Find("Toggle/Text_Zone");
        mLeftLayoutGroup = mTarget.transform.Find("Toggle").GetComponent<VerticalLayoutGroup>();
        mToggle = mTarget.transform.Find("Toggle").GetComponent<Toggle>();

        if (isSingle())
        {
            padding_left = 56;
            mText = mTarget.transform.Find("Toggle/Text").GetComponent<MyText>();
        }
        else
        {
            padding_left = 137;
            mSpaceLayoutGroup = textZone.GetComponent<VerticalLayoutGroup>();
            mText = mTarget.transform.Find("Toggle/Text_Zone/Text_Content").GetComponent<MyText>();
            mSubText = mTarget.transform.Find("Toggle/Text_Zone/Text_Desc").GetComponent<MyText>();
            followObj = mTarget.transform.Find("Follow")?.gameObject;
            loadingObj = mTarget.transform.Find("Loading")?.gameObject;
        }

        hasFollow = (mTarget.transform.Find("Follow") != null);
        hasLoading = (mTarget.transform.Find("Loading") != null);
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        EditorGUI.BeginChangeCheck();
        mText.text = EditorGUILayout.TextField("主文字：", mText.text);
        if (textEditor == null)
            textEditor = CreateEditor(mText) as MyTextEditor;
        foldoutText = textEditor.CreateStyleEditor(foldoutText);
        if (isSingle())
        {

        }
        else
        {
            mSubText.text = EditorGUILayout.TextField("副文字：", mSubText.text);
            if (subTextEditor == null)
                subTextEditor = CreateEditor(mSubText) as MyTextEditor;
            foldoutSubText = subTextEditor.CreateStyleEditor(foldoutSubText);
            hasFollow = EditorGUILayout.Toggle("有跟随图标：", hasFollow);
            hasLoading = EditorGUILayout.Toggle("有转圈图标：", hasLoading);
        }
        mToggle.isOn = EditorGUILayout.Toggle("isOn：", mToggle.isOn);
        //mTarget.clickNoChange = EditorGUILayout.Toggle("点击不切换状态：", mTarget.clickNoChange);
        if (EditorGUI.EndChangeCheck())
        {
            mTarget.SetVoiceText(mText.text);
            mLeftLayoutGroup.padding.left = padding_left;
            if (isSingle())
            {

            }
            else
            {
                mSpaceLayoutGroup.spacing = padding_space;
                mSubText.gameObject.SetActive(!string.IsNullOrEmpty(mSubText.text));
                RefreshObj();
            }

            EditorUtility.SetDirty(mTarget);
        }
        RefreshFollowPos();;
    }

    protected override string GetDataJson()
    {
        var data = new HMI_ToggleData();

        data.TextData = mText.GetData();
        if (!isSingle())
            data.SubTextData = mSubText.GetData();

        data.HasFollow = hasFollow;
        data.HasLoading = hasLoading;

        string json = JsonUtility.ToJson(data, true);
        return json;
    }

    protected override void SetData(string json)
    {
        HMI_ToggleData data = JsonUtility.FromJson<HMI_ToggleData>(json);

        if (data.TextData != null)
        {
            MyTextData textData = data.TextData;
            mText.SetData(textData);
        }
        if (!isSingle() && mSubText != null && data.SubTextData != null)
        {
            MyTextData subTextData = data.SubTextData;
            mSubText.SetData(subTextData);
        }
        else
        {
            hasFollow = data.HasFollow;
            hasLoading = data.HasLoading;
            RefreshObj();
        }

        base.SetData(json);
        EditorUtility.SetDirty(mTarget);
    }

    //刷新跟随图标位置
    private void RefreshFollowPos()
    {
        if (!isSingle() && hasFollow && followObj != null)
        {
            var mFollowRect = followObj.GetComponent<RectTransform>();
            if (mText.cachedTextGeneratorForLayout.lineCount > 1)
            {
                var lineInfo = mText.cachedTextGeneratorForLayout.lines[1];
                var charInfo = mText.cachedTextGeneratorForLayout.characters[lineInfo.startCharIdx - 1];
                mFollowRect.anchoredPosition = new Vector2(padding_left + charInfo.cursorPos.x + charInfo.charWidth + follow_space, -8f);
            }
            else if (mText.cachedTextGeneratorForLayout.characters.Count > 0)
            {
                var charInfo = mText.cachedTextGeneratorForLayout.characters[mText.cachedTextGeneratorForLayout.characters.Count - 1];
                mFollowRect.anchoredPosition = new Vector2(padding_left + charInfo.cursorPos.x + charInfo.charWidth + follow_space, mSubText.gameObject.activeSelf ? -8f : -30.5f);
            }
        }
    }

    private void RefreshObj()
    {
        if (hasFollow)
        {
            if (followObj == null)
            {
                followObj = HMI_Slider_Editor.LoadMiniWidget(mTarget.transform, "Follow");

                var mFollowRect = followObj.transform as RectTransform;
                mLeftLayoutGroup.padding.right = hasFollow ? ((int)mFollowRect.sizeDelta.x + follow_space) : 0;
            }
        }
        else
        {
            var arrowGo = mTarget.transform.Find("Follow");
            if (arrowGo != null)
            {
                followObj = null;
                DestroyImmediate(arrowGo.gameObject);
            }

            //mLeftLayoutGroup.padding.right = 0;
        }
        if (hasLoading)
        {
            if (loadingObj == null)
            {
                loadingObj = HMI_Slider_Editor.LoadMiniWidget(mTarget.transform, "Loading");
            }
        }
        else
        {
            var arrowGo = mTarget.transform.Find("Loading");
            if (arrowGo != null)
            {
                loadingObj = null;
                DestroyImmediate(arrowGo.gameObject);
            }

        }
    }
    private bool isSingle()
    {
        return mTarget.widgetType == WidgetType_ToggleSingle;
    }
}

[Serializable]
public class HMI_ToggleData
{
    public MyTextData TextData;  //文字
    public MyTextData SubTextData;  //副文字
    public bool HasFollow;  //是否有跟随图标
    public bool HasLoading;  //是否有右箭头
}

